package com.cgc.venus.server.job.service;
import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.job.inter.SmService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import smscenter.WsSendSmsStub;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class SmServiceImpl extends AbstractBaseService implements SmService {
    private static final Logger logger = LogManager.getLogger(SmServiceImpl.class);
    @Override
    public boolean sendShortMessage(String[] phones, String content, String topic) throws VenusException {
        try {
            if(StringUtil.isEmpty(topic)){
                return false;
            }
            if(StringUtil.isEmpty(content)){
                return false;
            }
            if(phones==null||phones.length==0){
                return false;
            }
            content="【"+topic+"】"+content;

            //1.发送短信
            //将手机号码数组转化为字符串类型
            String phone="";
            for(int i=0;i<phones.length;i++){
                phone+=phones[i];
                phone+=",";
            }
            //去掉phone字符串的最后一个逗号
            phone = phone.substring(0, phone.length()-1);
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            WsSendSmsStub ws=new WsSendSmsStub();
            WsSendSmsStub.OutSendSms oss=new WsSendSmsStub.OutSendSms();
            oss.setStrPhone(phone);
            oss.setStrContent(content);
            oss.setSendDate(dateFormat1.format(new Date()));
            oss.setCallerid("call001");
            oss.setAccesskey("123456");
            oss.setSendid("xxx");
            oss.setSourcecode("xxx");
            ws.outSendSms(oss);
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return false;
        }
    }
}